//
//  ControlTemplateViewTextController.m
//  Uhomebk
//
//  Created by 揭婧雯 on 17/3/3.
//  Copyright © 2017年 com.segi. All rights reserved.
//

#import "ControlTemplateViewTextController.h"

#pragma mark -
#pragma mark 单行文本框控件【text】
@interface ControlTemplateViewTextController () {
    UITextField* textField;
    CGSize titleSize;
}
@property (nonatomic, retain) NSString* defaultValue;
@end

@implementation ControlTemplateViewTextController

ControlTemplateViewTextController* sharedManagerTextInter = nil;

+ (ControlTemplateViewTextController*)sharedManagerText
{
    sharedManagerTextInter = [[self alloc] init];
    
    return sharedManagerTextInter;
}

/**
 *  初始化变量
 */
- (void)initVar
{
    _valueStr = @"";
    _param_id = [SEGUtils ValueToString:[_params valueForKey:@"attrInstId"]];
    _optional = [SEGUtils ValueToString:[_params valueForKey:@"required"]];
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.isNeedAdpterHotspot = YES;
    
    [self initVar];
    
    _bgMainView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, [[UIScreen mainScreen] bounds].size.width, TEMPLATE_HEIGHT)];
    _bgMainView.backgroundColor = kColor_W;
    self.tempBgView = _bgMainView;
    [self.view addSubview:_bgMainView];
    
    maxString = [[_params valueForKey:@"contentLengthLimit"] integerValue];
    
    UILabel* titleLabel = [[UILabel alloc] init];
    [titleLabel extendAttributedString:[_params valueForKey:@"title"] lineSpace:LINESPACE_6 font:kFont_16];
    titleLabel.numberOfLines = 0;
    titleLabel.lineBreakMode = NSLineBreakByCharWrapping;
    [titleLabel setFont:kFont_16];
    [titleLabel setTextColor:kColor_1];
    [_bgMainView addSubview:titleLabel];
    [titleLabel sizeToFit];
    [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.bgMainView.mas_left).offset(HEADSPACE);
        make.centerY.equalTo(self.bgMainView.mas_centerY);
        make.width.mas_equalTo(TITLE_WIDTH);
    }];
    titleSize = [[_params valueForKey:@"title"] getAttributionHeightWithlineSpace:LINESPACE_6 font:kFont_16 width:TITLE_WIDTH];
    ////////判断defaultValue是否有值
    if ([[_params valueForKey:@"attrValue"] length] > 0) {
        _defaultValue = [_params valueForKey:@"attrValue"];
    }
    
    ////////是否可读 0-不显示 1-显示
    if ([[SEGUtils ValueToString:[_params valueForKey:@"readable"]] isEqualToString:@"0"]) {
        _bgMainView.frame = CGRectMake(0, 0, CGRectGetWidth(_bgMainView.bounds), 0);
    }
    else {
        //线条
        UIView* lineView = [[UIView alloc] initWithFrame:CGRectMake(LINEMARGIN_LEFT, CGRectGetHeight(_bgMainView.bounds) - 0.5, CGRectGetWidth(_bgMainView.bounds), 0.5)];
        lineView.backgroundColor = kColor_L2;
        lineView.tag = 8888;
        [_bgMainView addSubview:lineView];
    }
    
    
    ////////是否必填 //是否可读 1-必须 0-可不填
    _optional = [SEGUtils ValueToString:[_params valueForKey:@"required"]];
    
    ////////是否可写 //是否可读 0-不可写 1-可写
    if ([[SEGUtils ValueToString:[_params valueForKey:@"writable"]] isEqualToString:@"0"])
    {
        //不可写但是可读
        if ([[SEGUtils ValueToString:[_params valueForKey:@"readable"]] isEqualToString:@"1"]) {
            
            UILabel* alabel = [[UILabel alloc] initWithFrame:CGRectMake(CONTENTMARGIN_LEFT, 0, CGRectGetWidth(_bgMainView.bounds) - CONTENTMARGIN_LEFT - CONTENTMARGIN_RIGHT, TEMPLATE_HEIGHT)];
            [alabel setText:[_params valueForKey:@"attrValue"]];
            [alabel extendAttributedString:[_params valueForKey:@"attrValue"] lineSpace:LINESPACE_6 font:kFont_16];
            alabel.lineBreakMode = NSLineBreakByCharWrapping;
            alabel.numberOfLines = 0;
            [alabel setFont:kFont_16];
            [alabel setTextColor:kColor_1];
            [_bgMainView addSubview:alabel];
            
            CGSize size = [[_params valueForKey:@"attrValue"] getAttributionHeightWithlineSpace:LINESPACE_6 font:kFont_16 width:CGRectGetWidth(alabel.bounds)];
            size.height = size.height > 24 ? size.height : 24;
            size.height = titleSize.height > size.height ? titleSize.height : size.height;
            alabel.frame = CGRectMake(CONTENTMARGIN_LEFT, 0, CGRectGetWidth(alabel.bounds), TEMPLATE_HEIGHT -24 + size.height);
            _bgMainView.frame = CGRectMake(0, 0, CGRectGetWidth(_bgMainView.bounds), CGRectGetHeight(alabel.bounds));
            UIView* lineView = [_bgMainView.subviews objectAtIndex:1];
            lineView.frame = CGRectMake(lineView.frame.origin.x, CGRectGetHeight(_bgMainView.bounds) - 0.5, CGRectGetWidth(lineView.bounds), CGRectGetHeight(lineView.bounds));
        }
    }
    //可写
    else {
        textField = [[UITextField alloc] initWithFrame:CGRectMake(CONTENTMARGIN_LEFT, 0, CGRectGetWidth(_bgMainView.bounds) - CONTENTMARGIN_LEFT - CONTENTMARGIN_RIGHT, TEMPLATE_HEIGHT)];
        textField.delegate = self;
        textField.returnKeyType = UIReturnKeyDone;
        textField.placeholder = [_params valueForKey:@"markedWords"];
        [textField setAttributedPlaceholder:[[NSAttributedString alloc] initWithString:textField.placeholder ?: @"" attributes:@{NSForegroundColorAttributeName:kColor_C}]];
        [textField setTextColor:kColor_1];
        [textField setFont:kFont_16];
        if (_defaultValue && _defaultValue.length > 0) {
            textField.text = _defaultValue;
        }
        [_bgMainView addSubview:textField];
        textField.inputAccessoryView = [self onKeyBoardFinishedButton];
        
        titleSize.height = titleSize.height > 24 ? titleSize.height : 24;
        textField.frame = CGRectMake(CONTENTMARGIN_LEFT, 0, CGRectGetWidth(_bgMainView.bounds) - CONTENTMARGIN_LEFT - CONTENTMARGIN_RIGHT, TEMPLATE_HEIGHT -24 + titleSize.height);
        _bgMainView.frame = CGRectMake(0, 0, CGRectGetWidth(_bgMainView.bounds), CGRectGetHeight(textField.bounds));
        UIView* lineView = [_bgMainView.subviews objectAtIndex:1];
        lineView.frame = CGRectMake(lineView.frame.origin.x, CGRectGetHeight(_bgMainView.bounds) - 0.5, CGRectGetWidth(lineView.bounds), CGRectGetHeight(lineView.bounds));
    }
}

/**
 *  点击键盘上完成按钮收回键盘
 *
 *  @param sender 按钮实例
 */
- (void)onKeyBoardFinishedButton:(CustomButtonView*)sender
{
    [textField resignFirstResponder];
}

/**
 *  单行文本内容变动回调
 *
 *  @param textField2 文本框实例
 *  @param range      文本范围
 *  @param string     需要代替的字符串
 *
 *  @return 返回真值
 */
//-(BOOL)textField:(UITextField *)textField2 shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
//{
//    if ([[textField2 text] length]>0)
//    {
//        if ([[textField2 text] length]>maxString)
//        {
//            NSString *contentStr = [[textField text] substringToIndex:maxString];
//            textField2.text = contentStr;
//            _optional = @"0";
//
//
//        }
//
//
//        if ([[_params valueForKey:@"expressCode"] length]>0)
//        {
//            if ([self isExpressCode:[textField2 text] expressStr:[_params valueForKey:@"expressCode"]])
//            {
//                _valueStr = [textField2 text];
//                _optional = @"0";
//            }
//            else
//            {
//                _valueStr = @"";
//                _optional = @"1";
//                [textField2 setText:@""];
//                NSString *msg = [NSString stringWithFormat:@"%@是无效的",[_params valueForKey:@"title"]];
//                [[SystemUtils sharedManager] onLoadDataStatusMessage:msg View:[[UIApplication sharedApplication] keyWindow]];
//                _optional = @"1";
//            }
//        }
//
//    }
//
//
//    return YES;
//}

- (void)textFieldDidBeginEditing:(UITextField*)textField2
{
    [[NSNotificationCenter defaultCenter] postNotificationName:UIKeyboardDidShowNotification object:nil userInfo:[NSDictionary dictionaryWithObjectsAndKeys:textField2, @"object", nil]];
}

/**
 *  单行文本内容结束时回调
 *
 *  @param textField2 文本框实例
 */
- (void)textFieldDidEndEditing:(UITextField*)textField2
{
    [[NSNotificationCenter defaultCenter] postNotificationName:UIKeyboardDidHideNotification object:nil userInfo:[NSDictionary dictionaryWithObjectsAndKeys:textField2, @"object", nil]];
    
    if ([[textField2 text] length] > 0) {
        if ([[_params valueForKey:@"expressCode"] length] > 0) {
            if ([SEGUtils isExpressCode:[textField2 text] expressStr:[_params valueForKey:@"expressCode"]]) {
                _valueStr = [textField2 text];
                // [需求12430]属性为必填时需要校验是否为空格
                NSString *fillterValueStr = [textField2.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
                if (fillterValueStr.length > 0) {
                    _optional = @"0";
                }
            }
            else {
                _valueStr = @"";
                _optional = @"1";
                [textField2 setText:@""];
                NSString* msg = [NSString stringWithFormat:@"%@是无效的", [_params valueForKey:@"title"]];
                [SEGAlertView showTmepMessageWithMessage:msg];
            }
        }
    }
}

/**
 *  单行文本框结束编辑时回调
 *
 *  @param textField2 文本框实例
 *
 *  @return 返回真值
 */
- (BOOL)textFieldShouldEndEditing:(UITextField*)textField2
{
    _valueStr = [textField2 text];
    
    if ([_valueStr length] == 0) //如果没有填写内容
    {
        _optional = [SEGUtils ValueToString:[_params valueForKey:@"required"]];
    }
    else {
        if ([[_params valueForKey:@"expressCode"] length] > 0) //有正则验校
        {
            if ([SEGUtils isExpressCode:[textField2 text] expressStr:[_params valueForKey:@"expressCode"]]) //验较成功，置为非必填
            {
                _valueStr = [textField2 text];
                // [需求12430]属性为必填时需要校验是否为空格
                NSString *fillterValueStr = [textField2.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
                if (fillterValueStr.length > 0) {
                    _optional = @"0";
                }
            }
            else { //验较不通过置为必填，提示需要填写，并将填写值置空
                _valueStr = @"";
                _optional = @"1";
                [textField2 setText:@""];
                NSString* msg = [NSString stringWithFormat:@"%@是无效的", [_params valueForKey:@"title"]];
                [SEGAlertView showTmepMessageWithMessage:msg];
            }
        }
        else { //没有正则验校，置为非必填
            
            // [需求12430]属性为必填时需要校验是否为空格
            NSString *fillterValueStr = [textField2.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
            if (fillterValueStr.length > 0) {
                _optional = @"0";
            }
        }
    }
    
    return YES;
}

/**
 *  给单行文本控件填充值
 *
 *  @param valueStr 接收字符串值
 */
- (void)paddingTextView:(NSString*)valueStr
{
    _valueStr = valueStr;
    [textField setText:valueStr];
    // [需求12430]属性为必填时需要校验是否为空格
    NSString *fillterValueStr = [valueStr stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
    if ([fillterValueStr length] > 0) {
        _optional = @"0";
    } else {
        _optional = @"1";
    }
}

@end


